package com.cgwx.tmservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.cgwx.tmservice.config.mybatisplus.RequestDynamicTableNameHelper;
import com.cgwx.tmservice.entity.bean.TmFrameConfigIdToIdentified;
import com.cgwx.tmservice.entity.tm.TmInfoLogDate;
import com.cgwx.tmservice.entity.vo.TmReplayDataVo;
import com.cgwx.tmservice.mapper.TmInfoLogDateMapper;
import com.cgwx.tmservice.service.TmInfoLogDateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 遥测原码记录表 服务实现类
 * </p>
 *
 * @author GaoWei
 * @since 2023-08-31
 */
@Service
public class TmInfoLogDateServiceImpl extends ServiceImpl<TmInfoLogDateMapper, TmInfoLogDate> implements TmInfoLogDateService {

    @Autowired
    private TmInfoLogDateMapper tmInfoLogDateMapper;

//    创建每日遥测原码记录表
    @Override
    public int createTable(String tmInfoLogDateTableName) {
        return tmInfoLogDateMapper.createTable(tmInfoLogDateTableName);
    }

//    在每日遥测原码记录表中插入一条数据
    @Override
    public TmInfoLogDate saveTmInfoLogDate(String tmInfoLogTableName, TmInfoLogDate tmInfoLogDate) {
        if(StringUtils.isNotBlank(tmInfoLogTableName)){
            RequestDynamicTableNameHelper.setRequestData(tmInfoLogTableName);
        }
        tmInfoLogDateMapper.insert(tmInfoLogDate);
//        返回插入的类，主要取其id值，用于存储解析表的一个字段
        return tmInfoLogDate;
    }

//    导出原码记录
    @Override
    public List<TmInfoLogDate> getTmLogReplayList(TmReplayDataVo tmReplayDataVo) {
        //        设置待查询的表名
        String tmInfoLogDateTableName = "tm_info_log_" + tmReplayDataVo.getReplayDate();
        if(StringUtils.isNotBlank(tmInfoLogDateTableName)){
            RequestDynamicTableNameHelper.setRequestData(tmInfoLogDateTableName);
        }
        QueryWrapper<TmInfoLogDate> queryWrapper = new QueryWrapper<>();
//        回放时间，需要加日期
        String startTime = tmReplayDataVo.getReplayDate().substring(0,4) + "-" + tmReplayDataVo.getReplayDate().substring(4,6)
                + "-" + tmReplayDataVo.getReplayDate().substring(6,8) + " " + tmReplayDataVo.getReplayStartTime();
        String endTime = tmReplayDataVo.getReplayDate().substring(0,4) + "-" + tmReplayDataVo.getReplayDate().substring(4,6)
                + "-" + tmReplayDataVo.getReplayDate().substring(6,8) + " " + tmReplayDataVo.getReplayEndTime();
        queryWrapper.between("gmt_create",startTime,endTime);
//        回放通路
        queryWrapper.eq("dataChannel",tmReplayDataVo.getReplayChannel());
//        根据id得到遥测帧识别码
        ArrayList<String> replayTmFrameIdentifiersList = new ArrayList<>();
//        此处得到的为遥测帧配置id
        ArrayList<String> replayTmFrameIdList = tmReplayDataVo.getReplayTmList();
        for (String tmFrameId : replayTmFrameIdList) {
            String tmFrameIdentifiers = TmFrameConfigIdToIdentified.TM_FRAME_ID_IDENTIFIED.get(tmFrameId);
            replayTmFrameIdentifiersList.add(tmFrameIdentifiers);
        }
        queryWrapper.in("tmframe_identifiers",replayTmFrameIdentifiersList);
        return tmInfoLogDateMapper.selectList(queryWrapper);
    }
}
